package com.briup.MR.Combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.Combiner.AvgTempCombinerMR -D input=/user/hdfs/wea.txt -D output=/user/hdfs/avg_result
 */
public class AvgTempCombinerMR
        extends Configured implements Tool {
    static class AvgTempCombinerMRMapper
            extends Mapper<LongWritable,Text, Text,SumValue>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] strs=value.toString().split(",");
            SumValue v=
                    new SumValue(Double.parseDouble(strs[1]),
                            1);
            context.write(new Text(strs[0]),v);
        }
    }

    /*
    map->
    key     value
    1992    SumValue(23,1)
    1993    SumValue(22,1)
    没有combiner
    进入reduce
    key     value
    1992    [SumValue(23,1)........]
     */
    /*
        添加combiner
        1.combiner添加和删除不能影响原来的程序
        2.combiner输入的键值是map的输出键值
        ，combiner输出的键值是reduce的输入键值，
        map输出的键值是reduce输入的键值
     */
    static class AvgTempCombinerMRCombiner
            extends Reducer<Text,SumValue,Text,SumValue>{
        @Override
        protected void reduce(Text key, Iterable<SumValue> values, Context context) throws IOException, InterruptedException {
            int nums=0;
            double sum_temp=0;
            for(SumValue val:values){
                nums+=val.getNum();
                sum_temp+=val.getTemp();
            }
            context.write(key,new SumValue(sum_temp,nums));
        }
    }
    static class AvgTempCombinerMRReduce
            extends Reducer<Text,SumValue,Text, DoubleWritable>{
        @Override
        protected void reduce(Text key, Iterable<SumValue> values, Context context) throws IOException, InterruptedException {
            int nums=0;
            double sum_temp=0;
            for(SumValue val:values){
                nums+=val.getNum();
                sum_temp+=val.getTemp();
            }
            context.write(key,
                    new DoubleWritable(sum_temp/nums));
        }
    }

    public int run(String[] args) throws Exception {
        Configuration conf=getConf();
        String input=conf.get("input");
        String output=conf.get("output");

        Job job=Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("avg_temp");

        job.setMapperClass(AvgTempCombinerMRMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(SumValue.class);

        //设置combiner
        job.setCombinerClass(AvgTempCombinerMRCombiner.class);

        job.setNumReduceTasks(2);

        job.setReducerClass(AvgTempCombinerMRReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        TextInputFormat.addInputPath(job,new Path(input));
        TextOutputFormat.setOutputPath(job,new Path(output));

        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(
                new ToolRunner().run(
                        new AvgTempCombinerMR(),args));
    }
}
